function setSession(k, v, expire = null) {
  const MINUTE = 60 * 1000;
  const obj = {
    data: v,
    startTime: new Date().getTime(),
    expire: (expire && expire * MINUTE) || null
  };
  localStorage.setItem(k, JSON.stringify(obj));
}

function getSession(k) {
  const storage = JSON.parse(localStorage.getItem(k));
  // localstorage is null
  if (!storage) return false;

  const { expire, startTime, data } = storage;
  const now = new Date().getTime();
  if (expire && now - startTime > expire) {
    //localstorage expired
    localStorage.removeItem(k);
    return false;
  } else return data;
}

function getSessionKey(mutation) {
  const arr = mutation.split("_");
  return arr[arr.length - 1].toLowerCase();
}

export { setSession, getSession, getSessionKey };
